package com.coocaa.ops.admin.v1.tab.provider.impl;

import com.coocaa.ops.admin.v1.dmp.biz.impl.DmpRegistryRlsInfoBizImpl;
import com.coocaa.ops.admin.v1.dmp.util.DmpRegistryRlsUtil;
import com.coocaa.ops.admin.v1.plugin.provider.IPluginUpdateProvider;
import com.coocaa.ops.admin.v1.poster.biz.IGlobalPictureBiz;
import com.coocaa.ops.admin.v1.tab.biz.impl.TabPanelRlsBizImpl;
import com.coocaa.ops.admin.v1.tab.convert.TabSaveConverter;
import com.coocaa.ops.admin.v1.tab.model.intenal.TabBatchSaveModel;
import com.coocaa.ops.admin.v1.tab.provider.ITabInfoRlsUpdateProvider;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.tab.constant.TabTypeConstants;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import com.coocaa.ops.common.core.biz.tab.entity.TabPanelConfigInfo;
import com.coocaa.ops.common.core.biz.tab.entity.TabPanelRlsEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author bjh
 * @description 版面信息原始表 关联信息更新提供者接口实现
 * @date : 2020-06-15 16:27:18
 */
@Service
public class TabInfoRlsUpdateProviderImpl implements ITabInfoRlsUpdateProvider {
    @Autowired
    private DmpRegistryRlsInfoBizImpl dmpRegistryRlsInfoBiz;
    @Autowired
    private TabPanelRlsBizImpl tabPanelRlsBiz;
    @Autowired
    private IPluginUpdateProvider pluginUpdateProvider;
    @Autowired
    private IGlobalPictureBiz globalPictureBiz;


    @Override
    public void saveRls(TabInfoEntity inputEntity, Integer userId) {
        if (TabTypeConstants.isNormal(inputEntity.getTabType())) {
            this.handleSaveNormalTab(inputEntity);
        } else if (TabTypeConstants.isPlugin(inputEntity.getTabType())) {
            this.handleSavePluginTab(inputEntity);
        }
    }

    @Override
    public void deleteAllRls(Integer id, Integer version) {
        DmpRegistryRlsUtil.deleteDmpRls(id, ResourceTypeEnum.TAB.getValue(), version);
        tabPanelRlsBiz.deleteRls(id, version);


    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private void handleSaveNormalTab(TabInfoEntity inputEntity) {
        TabBatchSaveModel saveModel = new TabBatchSaveModel();
        List<TabPanelConfigInfo> panelList = inputEntity.getPanelList();
        if (CollectionUtils.isEmpty(panelList)) {
            return;
        }
        for (TabPanelConfigInfo tabPanelConfigInfo : panelList) {
            if (Objects.equals(1, tabPanelConfigInfo.getIsDmpPanel())) {
                //如果是dmp内容
                handleDmpContent(inputEntity, tabPanelConfigInfo, saveModel);
            } else {
                TabPanelRlsEntity tabPanelRlsEntity = TabSaveConverter.getTabPanelRlsEntity(inputEntity, tabPanelConfigInfo);
                tabPanelRlsEntity.setPanelSeq(saveModel.getSubContainerSeq());
                saveModel.getFinalTabPanelRlsList().add(tabPanelRlsEntity);
            }
            saveModel.setSubContainerSeq(saveModel.getSubContainerSeq() + 1);
        }
        dmpRegistryRlsInfoBiz.saveBatchAvoidEmpty(saveModel.getFinalDmpList());
        tabPanelRlsBiz.saveBatchAvoidEmpty(saveModel.getFinalTabPanelRlsList());
    }

    private void handleDmpContent(TabInfoEntity inputEntity, TabPanelConfigInfo tabPanelConfigInfo, TabBatchSaveModel saveModel) {
        List<TabPanelConfigInfo> dmpPanelList = tabPanelConfigInfo.getDmpPanelList();
        if (CollectionUtils.isEmpty(dmpPanelList)) {
            return;
        }
        int contentSeq = 1;
        for (TabPanelConfigInfo dmpPanel : dmpPanelList) {
            DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = TabSaveConverter.getDmpRegistryRlsInfoEntity(inputEntity, dmpPanel);
            dmpRegistryRlsInfoEntity.setSubContainerSeq(saveModel.getSubContainerSeq());
            dmpRegistryRlsInfoEntity.setSubContainerContentSeq(contentSeq);
            saveModel.getFinalDmpList().add(dmpRegistryRlsInfoEntity);
            contentSeq++;
        }


    }


    private void handleSavePluginTab(TabInfoEntity tabInfoEntity) {
        if (tabInfoEntity == null) {
            return;
        }
        pluginUpdateProvider.saveTabPluginInfo(tabInfoEntity.getPluginInfo(), tabInfoEntity.getId(), tabInfoEntity.getCurrentVersion());
    }


}
